﻿//-----------------------------------------------------------------------------------------------------------
// Wbfs Engine (WBFSe) Project by Omega Frost 
// http://wbfse.codeplex.com/
//
// WBFSe is Licensed under the terms of the 
// Microsoft Reciprocal License (Ms-RL)
//-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace WBFSe.IO
{
    /// <summary>
    /// Network to host helper
    /// </summary>
    static class Ntoh
    {
        /// <summary>
        /// Reads an int16 from a buffer and converts to host order
        /// </summary>
        /// <param name="b">Buffer</param>
        /// <param name="offset">Offset in buffer</param>
        /// <returns>Value in host order</returns>
        internal static short ReadInt16(Byte[] b, int offset)
        {
            return IPAddress.NetworkToHostOrder(BitConverter.ToInt16(b, offset));
        }

        /// <summary>
        /// Reads an int32 from a buffer and converts to host order
        /// </summary>
        /// <param name="b">Buffer</param>
        /// <param name="offset">Offset in buffer</param>
        /// <returns>Value in host order</returns>
        internal static int ReadInt32(Byte[] b, int offset)
        {
            return IPAddress.NetworkToHostOrder(BitConverter.ToInt32(b, offset));
        }

        /// <summary>
        /// Reads an int64 from a buffer and converts to host order
        /// </summary>
        /// <param name="b">Buffer</param>
        /// <param name="offset">Offset in buffer</param>
        /// <returns>Value in host order</returns>
        internal static long ReadInt64(Byte[] b, int offset)
        {
            return IPAddress.NetworkToHostOrder(BitConverter.ToInt64(b, offset));
        }

        /// <summary>
        /// Reads an uint16 from a buffer and converts to host order
        /// </summary>
        /// <param name="b">Buffer</param>
        /// <param name="offset">Offset in buffer</param>
        /// <returns>Value in host order</returns>
        internal static ushort ReadUInt16(Byte[] b, int offset)
        {
            return (ushort)ReadInt16(b, offset);
        }

        /// <summary>
        /// Reads an uint32 from a buffer and converts to host order
        /// </summary>
        /// <param name="b">Buffer</param>
        /// <param name="offset">Offset in buffer</param>
        /// <returns>Value in host order</returns>
        internal static uint ReadUInt32(Byte[] b, int offset)
        {
            return (uint)ReadInt32(b, offset);
        }

        /// <summary>
        /// Reads an uint64 from a buffer and converts to host order
        /// </summary>
        /// <param name="b">Buffer</param>
        /// <param name="offset">Offset in buffer</param>
        /// <returns>Value in host order</returns>
        internal static ulong ReadUInt64(Byte[] b, int offset)
        {
            return (ulong)ReadInt64(b, offset);
        }
    }
}
